package ai.people.platform.archives.controller;

import ai.people.netmon.framework.domain.gang.construction.entity.*;
import ai.people.netmon.framework.domain.gang.construction.request.*;
import ai.people.netmon.framework.domain.gang.construction.valid.ValidationGroup;
import ai.people.netmon.framework.domain.gang.construction.vo.LabelLibTreeVO;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.archives.service.LabelLibService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * 标签库接口
 *
 * @author zhenglin
 * @date 2022/7/19
 */
@Validated
@RestController
@RequestMapping("/labelLib")
public class LabelLibController {

    @Autowired
    LabelLibService labelLibService;


    /**
     * 添加标签
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping
    public Result<Boolean> addLabel(@Validated @RequestBody LabelLibSaveOrUpdateRequest request) {
        labelLibService.addLabel(request);
        return ResultUtil.success();
    }

    /**
     * 更新标签
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PutMapping
    public Result<Boolean> updateLabel(@Validated(ValidationGroup.Update.class) @RequestBody LabelLibSaveOrUpdateRequest request) {
        labelLibService.updateLabel(request);
        return ResultUtil.success();
    }

    /**
     * 删除标签
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteLabel(@PathVariable("id") Long id) {
        labelLibService.deleteLabel(id);
        return ResultUtil.success();
    }

    /**
     * 列表标签树
     *
     * @return {@link Result}<{@link List}<{@link LabelLibTreeVO}>>
     */
    @GetMapping("/tree")
    public Result<List<LabelLibTreeVO>> listLabelTree() {
        List<LabelLibTreeVO> list = labelLibService.listLabelTree();
        return ResultUtil.success(list);
    }

    /**
     * 账号-获取列表
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link LabelLibAccount}>>
     */
    @PostMapping("/account/list")
    public Result<PageResponse<LabelLibAccount>> listAccount(@Validated @RequestBody LabelLibAccountRequest request) {
        PageResponse<LabelLibAccount> pageResponse = labelLibService.listAccount(request);
        return ResultUtil.success(pageResponse);
    }

    /**
     * 账号-添加账号
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/account")
    public Result<Boolean> addAccount(@Validated @RequestBody LabelLibAccountSaveOrUpdateRequest request) {
        labelLibService.addAccount(request);
        return ResultUtil.success();
    }

    /**
     * 账号-上传Excel文件
     *
     * @param file       文件
     * @param labelLibId 标签ID
     * @return {@link Result}<{@link Boolean}>
     * @throws IOException ioexception
     */
    @PostMapping("/account/upload")
    public Result<Boolean> uploadAccount(@RequestParam("file") MultipartFile file, @NotNull(message = "标签ID不能为空") Long labelLibId) throws IOException {
        List<Integer> errorLine = labelLibService.uploadAccount(file, labelLibId);
        return ResultUtil.success(errorLine);
    }

    /**
     * 账号-详情
     *
     * @param id id
     * @return {@link Result}<{@link LabelLibAccount}>
     */
    @GetMapping("/account/detail/{id}")
    public Result<LabelLibAccount> detailAccount(@PathVariable("id") Long id) {
        LabelLibAccount account = labelLibService.detailAccount(id);
        return ResultUtil.success(account);
    }

    /**
     * 账号-删除账号
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @DeleteMapping("/account/{id}")
    public Result<Boolean> delAccount(@PathVariable("id") Long id) {
        labelLibService.delAccount(id);
        return ResultUtil.success();
    }

    /**
     * 账号-推荐数据列表
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link LabelLibAccountRecommend}>>
     */
    @PostMapping("/accountRecommend/list")
    public Result<PageResponse<LabelLibAccountRecommend>> listLabelAccountRecommend(@Validated @RequestBody LabelLibAccountRequest request) {
        PageResponse<LabelLibAccountRecommend> pageResponse = labelLibService.listLabelAccountRecommend(request);
        return ResultUtil.success(pageResponse);
    }

    /**
     * 账号-推荐数据添加训练
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/accountRecommend/addToTrain")
    public Result<Boolean> labelAccountRecommendAddToTrain(@Validated @RequestBody LabelLibAccountRecommendTrainRequest request) {
        labelLibService.labelAccountRecommendAddToTrain(request);
        return ResultUtil.success();
    }


    /**
     * 关键词-获取
     *
     * @param labelLibId 标签自由id
     * @return {@link Result}<{@link LabelLibKeyword}>
     */
    @GetMapping("/keyword/get/{labelLibId}")
    public Result<LabelLibKeyword> getKeyword(@PathVariable("labelLibId") Long labelLibId) {
        LabelLibKeyword result = labelLibService.getKeyword(labelLibId);
        return ResultUtil.success(result);
    }

    /**
     * 关键词-更新关键词
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PutMapping("/keyword")
    public Result<Boolean> updateKeyword(@Validated(ValidationGroup.Update.class) @RequestBody LabelLibKeywordSaveOrUpdateRequest request) {
        labelLibService.updateKeyword(request);
        return ResultUtil.success();
    }

    /**
     * 关键词-删除关键词
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @DeleteMapping("/keyword/{id}")
    public Result<Boolean> delKeyword(@PathVariable("id") Long id) {
        labelLibService.delKeyword(id);
        return ResultUtil.success();
    }

    /**
     * 关键词-推荐数据列表
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link LabelLibKeywordRecommend}>>
     */
    @PostMapping("/keywordRecommend/list")
    public Result<PageResponse<LabelLibKeywordRecommend>> listLabelKeywordRecommend(@Validated @RequestBody LabelLibKeywordRequest request) {
        PageResponse<LabelLibKeywordRecommend> pageResponse = labelLibService.listLabelKeywordRecommend(request);
        return ResultUtil.success(pageResponse);
    }


    /**
     * 样本集-获取列表
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link LabelLibSample}>>
     */
    @PostMapping("/sample/list")
    public Result<PageResponse<LabelLibSample>> listSample(@Validated @RequestBody LabelLibSampleRequest request) {
        PageResponse<LabelLibSample> pageResponse = labelLibService.listSample(request);
        return ResultUtil.success(pageResponse);
    }


    /**
     * 样本集-添加样本
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/sample")
    public Result<Boolean> addSample(@Validated @RequestBody LabelLibSampleSaveOrUpdateRequest request) {
        labelLibService.addSample(request);
        return ResultUtil.success();
    }

    /**
     * 样本集-上传Excel文件
     *
     * @param file 文件
     * @return {@link Result}<{@link Boolean}>
     * @throws IOException ioexception
     */
    @PostMapping("/sample/upload")
    public Result<Boolean> uploadSample(@RequestParam("file") MultipartFile file, @NotNull(message = "标签ID不能为空") Long labelLibId) throws IOException {
        List<Integer> errorLine = labelLibService.uploadSample(file, labelLibId);
        return ResultUtil.success(errorLine);
    }

    /**
     * 样本集-删除样本集
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @DeleteMapping("/sample/{id}")
    public Result<Boolean> delSample(@PathVariable("id") Long id) {
        labelLibService.delSample(id);
        return ResultUtil.success();
    }

    /**
     * 样本集-推荐数据列表
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link LabelLibKeywordRecommend}>>
     */
    @PostMapping("/sampleRecommend/list")
    public Result<PageResponse<LabelLibSampleRecommend>> listSampleRecommend(@Validated @RequestBody LabelLibSampleRequest request) {
        PageResponse<LabelLibSampleRecommend> pageResponse = labelLibService.listSampleRecommend(request);
        return ResultUtil.success(pageResponse);
    }

    /**
     * 样本集-推荐数据添加训练
     *
     * @param request 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/sampleRecommend/addToTrain")
    public Result<Boolean> sampleRecommendAddToTrain(@Validated @RequestBody LabelLibSampleRecommendTrainRequest request) {
        labelLibService.sampleRecommendAddToTrain(request);
        return ResultUtil.success();
    }
}
